package com.durian.base.update.proxy.impl

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import com.durian.base.update.XUpdate
import com.durian.base.utils.AndroidInfoUtils

open class DownloadNotification(val context: Context) {

    private var notificationManager: NotificationManager? = null
    private var notificationBuilder: NotificationCompat.Builder? = null
    private val CHANNEL_ID = "xupdate_channel_id"
    private val CHANNEL_NAME = "xupdate_channel_name"
    private val DOWNLOAD_NOTIFY_ID = 123210

    init {
        notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    fun createNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
//设置绕过免打扰模式
//            channel.setBypassDnd(false);
//            //检测是否绕过免打扰模式
//            channel.canBypassDnd();
//            //设置在锁屏界面上显示这条通知
//            channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
//            channel.setLightColor(Color.GREEN);
//            channel.setShowBadge(true);
//            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            channel.enableVibration(false)
            channel.enableLights(false)
            notificationManager?.createNotificationChannel(channel)
        }
        notificationBuilder = getNotificationBuilder()
        notificationManager?.cancel(DOWNLOAD_NOTIFY_ID)
        notificationManager?.notify(DOWNLOAD_NOTIFY_ID, notificationBuilder?.build())
    }

    fun showProgressNotification(progress : Int){
        notificationBuilder?.let {
            val contentTitle = "正在下载：${AndroidInfoUtils.appName()}"
            it.setContentTitle(contentTitle)
                    .setContentText("${progress}%")
                    .setProgress(100, progress, false)
                    .setWhen(System.currentTimeMillis())
            val notification: Notification = it.build()
            notification.flags = Notification.FLAG_AUTO_CANCEL or Notification.FLAG_ONLY_ALERT_ONCE
            notificationManager?.notify(DOWNLOAD_NOTIFY_ID, notification)
        }
    }

    fun showSuccessNotification(){
        notificationManager?.cancel(DOWNLOAD_NOTIFY_ID)
    }

    fun showErrorNotification(){
        notificationManager?.cancel(DOWNLOAD_NOTIFY_ID)
    }

    private fun getNotificationBuilder(): NotificationCompat.Builder? {
        return NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle("开始下载")
                .setContentText("正在连接服务器…")
                .setSmallIcon(XUpdate.getSmallIcon())
//                .setLargeIcon(UpdateUtils.drawable2Bitmap(UpdateUtils.getAppIcon(this@OldDownloadService)))
                .setOngoing(true)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
    }

    fun destroy(){
        notificationManager?.cancel(DOWNLOAD_NOTIFY_ID)
        notificationManager = null
        notificationBuilder = null

    }

}